package com.zirun.config;

import com.zirun.handler.GlobalResponseHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName: AdviceConfig
 * @Author: Zirun.Xin
 * @Date: 2025/4/15 下午4:49
 * @Version: 1.0
 * @Description: 动态注册全局响应处理器的配置
 *
 * <p>通过 {@code @ConditionalOnProperty} 控制是否启用，并支持动态配置扫描的控制器包路径</p>
 */
@Configuration
public class GlobalResponseAdviceConfig {

    /**
     * 动态注册全局响应处理器 Bean
     *
     * <p>当配置 {@code api-result.advice.enabled=true} 时生效（默认启用）</p>
     * <p>实际功能由 {@link RestControllerAdviceProxy} 代理类实现</p>
     *
     * @return 代理类实例，包装原始 {@link GlobalResponseHandler} 功能
     * @see ConditionalOnProperty
     */
    @Bean
    @ConditionalOnProperty(name = "api-result.advice.enabled", havingValue = "true", matchIfMissing = true)
    public Object globalResponseAdvice() {
        return new RestControllerAdviceProxy();
    }

    /**
     * 内部代理类，动态指定控制器扫描范围
     *
     * <p>通过 {@code @RestControllerAdvice} 注解的 {@code basePackages} 属性，
     * 从配置 {@code api-result.advice.base-packages} 动态获取包路径（默认：com.zirun.controller）</p>
     *
     * <p>继承 {@link GlobalResponseHandler} 实现统一响应处理逻辑</p>
     */
    @RestControllerAdvice(basePackages = "${api-result.advice.base-packages:com.zirun.controller}")
    static class RestControllerAdviceProxy extends GlobalResponseHandler {

    }
}
